package com.fjnu.controller.repair;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fjnu.controller.InstrumentController;
import com.fjnu.controller.common.BaseController;
import com.fjnu.entities.FinalRepair;
import com.fjnu.entities.MainInstrument;
import com.fjnu.entities.User;
import com.fjnu.service.IFinalRepairService;
import com.fjnu.service.InstrumentService;
import com.util.StringUtil;

@Controller
public class FinalRepairController extends BaseController{
	private static Logger logger =LogManager.getLogger(FinalRepairController.class);
	@Autowired
	private IFinalRepairService finalRepairService;
	@Autowired
	private InstrumentService instrumentservice;
	@RequestMapping(value="/findFinalRepair")
	@ResponseBody
	@RequiresRoles("后勤维修人员")
	public Map<String,Object> findFinalRepair(Integer pageNo,Integer pageSize){
		logger.info("遍历维修单");
		if(!init().get(STATUS).equals(SUCCESS))
			return getData2Result();
		if(pageSize==null){
			pageSize=10;
		}
		if(pageNo==null){
			pageNo=1;
		}
		List<FinalRepair> list = finalRepairService.findFinalRepair(pageNo, pageSize);
		put("data",list);
		return getData2Result();
	}
	/**
	 * 维修部门处理一份维修单
	 * @param id
	 * @param status 2:维修成功 3:维修失败
	 * @return
	 */
	@RequiresRoles("后勤维修人员")
	@RequestMapping(value="/finalRepair")
	@ResponseBody
	public Map<String,Object> FinalRepair(Integer id,Integer status){
		logger.info("维修受理一份维修单");
		if(!init(id,status).get(STATUS).equals(SUCCESS))
			return getData2Result();
		Subject currentUser = SecurityUtils.getSubject();  
		 Session session = currentUser.getSession();
		User repairUser=(User) session.getAttribute("subject");
		FinalRepair finalRepair=finalRepairService.findUniqueFinalRepair(id);
		finalRepair.getExamRepair().getBaseRepair().setStatus(status);
		Date returnTime=new Date();
		finalRepair.getExamRepair().getBaseRepair().setReturnTime(returnTime);
		finalRepair.setRepairUser(repairUser);
		finalRepairService.updateFinalRepair(finalRepair);
		
		int instrumentId=finalRepair.getExamRepair().getBaseRepair().getMainInstrument().getId();
		MainInstrument maininstrument = instrumentservice.findUniqueInstrument(instrumentId);
		if(status==3){
			logger.info("维修不成功进行设备报废");
			maininstrument.setStatusQuo(maininstrument.DEAD);
			instrumentservice.updateInstrument(maininstrument);
		}
		else{
			logger.info("维修完成，设备正常");
			maininstrument.setStatusQuo(maininstrument.GOOD);
			instrumentservice.updateInstrument(maininstrument);
		}
		return getData2Result();
	}
	
}
